package com.oocl.homework.format.view;

import java.util.Set;

import com.oocl.homework.format.moel.Customer;

public class DataViewImpl implements DataView{
	
	private StringBuilder getLineView(Set<Customer> customers){
		StringBuilder stringBuilder = new StringBuilder();
		stringBuilder.append("id\tname\tsex\temail\t\tbirthday\r\n");
		for (Customer customer:customers) {
			stringBuilder.append(customer.getId()+"\t"+customer.getName()+"\t"+customer.getSex()+"\t"+customer.getEmail()+"\t"+customer.getBirthday()+"\r\n");
		}
		return stringBuilder;
	}
	
	
	private StringBuilder getXmlView(Set<Customer> customers) {
		if (!customers.isEmpty()) {
			StringBuilder stringBuilder = new StringBuilder();
			stringBuilder.append("<customers>\n");
			for(Customer customer:customers) {
				stringBuilder.append("\t<customer id=\""+customer.getId()+"\">\r\n");
				stringBuilder.append("\t\t<name>"+customer.getName()+"</name>\r\n");
				stringBuilder.append("\t\t<email>"+customer.getEmail()+"</email>\r\n");
				stringBuilder.append("\t\t<sex>"+customer.getSex()+"</sex>\r\n");
				stringBuilder.append("\t\t<birthday>"+customer.getBirthday()+"</birthday>\r\n");
				stringBuilder.append("\t</customer>\r\n");
			}
			stringBuilder.append("</customers>");
			return stringBuilder;
		}
		return null;
	}
	
	public StringBuilder getView(Set<Customer> customers,String format){
		switch (format) {
		case "xml":
			return getXmlView(customers);
		case "line":
			return getLineView(customers);
		default:
			return null;
		}
	}
	

}
